import { createRouter, createWebHashHistory } from 'vue-router'
import IndexView from '../views/IndexView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: IndexView
  },
  {
    path: '/type',
    name: 'type',
    component: () => import('../views/TypeView.vue')
  },
  {
    path: '/dangread',
    name: 'dangread',
    component: () => import('../views/DangReadView.vue')
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/ShopView.vue')
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/MyView.vue')
  },
  {
    path: '/search/:name?',
    name: 'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/:pathMatch(.*)',
    name: 'error',
    component: () => import('../views/ErrorView.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  var token = localStorage.getItem("token");
  if (to.meta.requireAuth) {
    if (token) {
      next();
    }
    else {
      next({name:"login"});
    }
  }else{
    next();
  }
});

export default router
